package com.giscloud.upload.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.giscloud.commons.model.Result;
import com.giscloud.commons.utils.JacksonUtil;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.upload.dao.AttachmentDao;
import com.giscloud.upload.entity.Attachment;
import com.giscloud.upload.entity.AttachmentExample;
import com.giscloud.upload.entity.dto.AttachmentDTO;
import com.giscloud.upload.service.AttachmentService;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("attachmentService")
@Log4j2
public class AttachmentServiceImpl implements AttachmentService {
    private final  AttachmentDao attachmentDao;

    @Autowired
    public AttachmentServiceImpl(AttachmentDao attachmentDao) {
        this.attachmentDao = attachmentDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(AttachmentDTO attachmentDTO) {
        log.debug("#queryPage parameter ={}", JacksonUtil.toJSONString(attachmentDTO));
        PageHelper.startPage(attachmentDTO.getPage(), attachmentDTO.getRows());
        AttachmentExample attachmentExample = new AttachmentExample();
        AttachmentExample.Criteria criteria = attachmentExample.createCriteria();
        List<Attachment> attachments = attachmentDao.selectByExample(attachmentExample);
        return PageInfo.of(attachments);
    }


    /**
     * 信息
     */
    @Override
    public Attachment getById(Integer attachmentId){
        log.debug("#getById parameter ={}", attachmentId);
        return attachmentDao.selectByPrimaryKey(attachmentId);
    }

    @Override
    public Attachment getByKey(String key) {
        AttachmentExample example = new AttachmentExample();
        example.createCriteria().andKeycodeEqualTo(key);
        return attachmentDao.selectOneByExample(example);
    }

    /**
     * 保存
     */
    @Override
    @Transactional
    public Result save(Attachment attachment){
        log.debug("#save parameter ={}", JacksonUtil.toJSONString(attachment));
        if(attachmentDao.insertSelective(attachment) > 0 ){
            return Result.success(attachment,"上传成功");
        }else{
            return Result.error("上传失败");
        }
    }

    /**
     * 修改
     */
    @Override
    @Transactional
    public Result updateById(Attachment attachment){
         log.debug("#updateById parameter ={}", JacksonUtil.toJSONString(attachment));
         AttachmentExample attachmentExample = new AttachmentExample();
         AttachmentExample.Criteria criteria = attachmentExample.createCriteria();
         criteria.andAttachmentIdEqualTo(attachment.getAttachmentId());
         if(attachmentDao.updateByExampleSelective(attachment,attachmentExample) > 0){
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override
    @Transactional
    public Result removeByIds(List<Integer> attachmentIds){
        log.debug("#removeByIds parameter ={}", JacksonUtil.toJSONString(attachmentIds));
        AttachmentExample attachmentExample = new AttachmentExample();
        AttachmentExample.Criteria criteria = attachmentExample.createCriteria();
        if(!CollectionUtil.isEmpty(attachmentIds)){
            criteria.andAttachmentIdIn(attachmentIds);
        }
        if(attachmentDao.deleteByExample(attachmentExample) > 0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}